home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / dbdoc.zip / DBTDOC.C < prev    next >
C/C++ Source or Header  |  1991-12-08  |  3KB  |  117 lines

  1. /*    Dbtdoc.c                                        */
  2.  
  3. /*    Version 1.0     December 8, 1991    */
  4. /*    Programmer:     Jay Parsons         */
  5.  
  6. #include <ctype.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9.  
  10. /****************************************************************************/
  11. /*                          definitions                                     */
  12. /****************************************************************************/
  13.  
  14. typedef unsigned long ulong;        /* just an abbreviation                 */
  15.  
  16. extern char *filespec, message[];
  17.  
  18. /****              ****  function prototypes  ****                       ****/
  19.  
  20. int dbtdoc ( char *filespec );
  21.  
  22. FILE * dbtopen ( char *filespec );
  23.  
  24. /****************************************************************************/
  25. /*                              dbtdoc                                      */
  26. /*  Principal routine of this module.                                                      */
  27. /*  Parameters:                                                             */
  28. /*      char *filespec  -- pointer to specification of the .dbt file        */
  29. /*     Returns 1 for error or 0, and fills external buffer "message"                 */
  30. /*                                                                          */
  31. /****************************************************************************/
  32.  
  33. int dbtdoc ( char *filespec )
  34. {
  35.     FILE *dbtfile;
  36.     unsigned blocksize = 512;
  37.     char foundname[9];
  38.     char *ptr;
  39.  
  40.     /*  open file and check for .dbt type       */
  41.  
  42.     if ( ( dbtfile = fopen( filespec, "rb" ) ) == NULL )
  43.     {
  44.         sprintf( message, "Unable to open file %s ", filespec );
  45.         printit( 1 );
  46.         return 1;                           /* can't open file  */
  47.     }
  48.  
  49.     /*  locate file name in spec and end it with null */
  50.  
  51.     ptr = strrchr( filespec, '\\' );
  52.     if ( ptr )
  53.         ptr++;
  54.     else
  55.         ptr = filespec;
  56.     memset( strrchr( filespec, '.' ), '\0', 1 );
  57.  
  58.     /*  check name starting at byte 8 of file   */
  59.  
  60.     if ( fseek( dbtfile, 8, SEEK_SET ) != 0 )
  61.     {
  62.         fclose( dbtfile );
  63.         sprintf( message, "No dBASE IV .dbt file header" );
  64.         printit( 1 );
  65.         return 1;                       /* can't reach name in .dbt     */
  66.     }
  67.     if ( fread( foundname, 9, 1, dbtfile ) != 1 )
  68.     {
  69.         fclose( dbtfile );
  70.         sprintf( message, "Can't read file header" );
  71.         printit( 1 );
  72.         return 1;                       /* can't read it                */
  73.     }
  74.     if ( strcmp( foundname, ptr ) !=0 )
  75.     {
  76.         fclose( dbtfile );
  77.         sprintf( message, "Wrong name in header--dBASE III+ type?" );
  78.         printit( 1 );
  79.         return 1;                       /* wrong name in file--corrupt  */
  80.     }
  81.  
  82. /*  get and check blocksize at bytes 20-21  */
  83.  
  84.     if ( fseek( dbtfile, 20, SEEK_SET ) != 0 )
  85.     {
  86.         fclose( dbtfile );
  87.         sprintf( message, "Can't reach blocksize word" );
  88.         printit( 1 );
  89.         return 1;                       /* can't reach size in .dbt     */
  90.     }
  91.  
  92.     if ( fread( &blocksize, 2, 1, dbtfile ) != 1 )
  93.     {
  94.         fclose( dbtfile );
  95.         sprintf( message, "Can't read blocksize from file" );
  96.         printit( 1 );
  97.         return 1;                       /* can't read block size        */
  98.     }
  99.  
  100.     if ( blocksize & 511 != 0 || blocksize > 32 * 512 )
  101.     {
  102.         fclose( dbtfile );
  103.         sprintf( message, "Illegal blocksize" );
  104.         printit( 1 );
  105.         return 1;                       /* illegal .dbt block size      */
  106.     }
  107.     else
  108.     {
  109.         fclose( dbtfile );
  110.         sprintf( message, "Blocksize is %d, %d bytes", blocksize/512, blocksize );
  111.         printit( 0 );
  112.         return 0;
  113.     }
  114. }
  115.  
  116. /*   EOF  */
  117.